/**
 * @FileName: Token.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 下午4:04:16
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

@TableBind(tableName = "user_token_map")
public class Token extends Model<Token>{
    private static final long  serialVersionUID = 1312507976941243069L;
    public static final Token  dao              = new Token();

    public static final String LOGIN_TIME       = "login_time";
    public static final String TOKEN            = "token";

    public boolean ifTokenExist(String userName){
        String sql = "select * from user_token_map where username=?";

        List<Token> tokenList = dao.find(sql, userName);
        if(tokenList.size() > 0){
            return true;
        } else{
            return false;
        }
    }

    public Token getTokenByUserName(String username){
        String sql = "select * from user_token_map where user_token_map.username=? order by login_time desc";

        return dao.findFirst(sql, username);
    }

    public int getOrgIdByToken(String tokenStr){
        String sql = "select org_id from member_org where member_org.user_name=(select username from user_token_map where token=?"
                + ");";
        Record r = Db.findFirst(sql, tokenStr);
        return r == null ? -1 : r.getInt("org_id");
    }

    public String getUsernameByToken(String tokenStr){
        String sql = "select username from user_token_map where token=?;";
        Record r = Db.findFirst(sql, tokenStr);
        return r == null ? null : r.getStr("username");
    }

    public String getNotExpiredUsernameByToken(String tokenStr){
        String sql = "select username from user_token_map where token=? and is_expired=0;";
        Record r = Db.findFirst(sql, tokenStr);
        return r == null ? null : r.getStr("username");
    }

    public long getLoginTime(String tokenStr){
        String sql = "select login_time from user_token_map where token=? and is_expired=0;";
        Record r = Db.findFirst(sql, tokenStr);
        return r == null ? 0 : r.getDate("login_time").getTime();
    }

    public int getUserIdByToken(String tokenStr){

        Record r = Db.findFirst(
                                "select member_org.id from member_org,user_token_map where user_token_map.token=? and member_org.user_name=user_token_map.username",
                                tokenStr);
        return r == null ? 0 : r.getInt("id");
    }

    /**
     * @Title: getDayActiveUserCount
     * @Description:获取客户端日活跃用户数
     * @return Long
     */
    public Long getDayActiveUserCount(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());

        String sql = " select count(DISTINCT(username)) as dau from user_token_map where date_format(login_time, '%Y-%m-%d') = ? ";
        List<Record> dauList = Db.find(sql, today);
        Long dauCount = dauList.get(0).get("dau");

        return dauCount;
    }

    /**
     * @Title: getDayActiveStoreCount
     * @Description:日活跃店铺数
     * @return Long
     */
    public Long getDayActiveStoreCount(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());

        String sql = " select count(1) as dau from (select u.username, org.org_id from "
                + " (select DISTINCT(username) from user_token_map where date_format(login_time, '%Y-%m-%d') = ? ) as u, "
                + " member_org org where u.username = org.user_name group by org.org_id) as store ";
        Record dau = Db.findFirst(sql, today);
        Long dauCount = dau.get("dau");

        return dauCount;
    }

    public Long getLoginUserCount(){
        String sql = " select count(1) as cnt from user_token_map ";
        List<Record> cntList = Db.find(sql);
        Long userCnt = cntList.get(0).get("cnt");

        return userCnt;
    }

    /**
     * @Title: getTokenByUserid
     * @Description:若userid未登录将返回空，或者是很早之前的登陆，则返回一个已经失效的token
     * @param userid
     * @return String
     */public String getTokenByUserid(int userid){
        String sql = "select t.token from user_token_map t,member_org m where m.id =? and m.user_name = t.username order by t.login_time desc;";
        Record r = Db.findFirst(sql, userid);
        return r == null ? null : r.getStr("token");
    }


}
